package com.asia5b.a5mall.middleware.logic.viewmodel.member

import androidx.lifecycle.MutableLiveData
import com.asia5b.a5mall.middleware.R
import com.asia5b.a5mall.middleware.base.SimpleNetParams
import com.asia5b.a5mall.middleware.base.SimpleViewModel
import com.asia5b.a5mall.middleware.api.CustomThrowable
import com.asia5b.a5mall.middleware.utils.showMessage
import io.reactivex.functions.Consumer

/**
 * Created by zhangyuncai on 2020/1/8.
 */
class NewAddressActivityVM:SimpleViewModel() {

    /**
     *
     * PC端
     * https://apimall.asiajy.com/buyer/members/address
    def_addr: 1 是否默认 1/默认
    gr: 86 国家区号
    ship_address_name:
    name: 武时亿1 收货人
    mobile: 13000000002 手机号
    region: 57978  地址编号
    addr: 天河大道一号 详细地址
    Company_home_school_or_others: 武时亿1别名
     *
     * @Note   添加地址
     * @param  data 地址
     */
    fun addAddress(map: HashMap<String, Any>) {

        val obserable = memberApi.addAddress(map)
        baseSubscribe(obserable, Consumer {
            showMessage(R.string.添加成功)
//            providerView().addAddressResult()
            addAddressResultLiveData.value=true
        })
    }
    val addAddressResultLiveData=MutableLiveData<Boolean>()

    /**
     * @Note   删除地址
     * @param  id 地址索引
     */
    fun deleteAddress(id: Int) {
        baseSubscribe(memberApi.deleteAddress(id), Consumer {
            showMessage(R.string.删除成功)
//            providerView().addAddressResult()
            addAddressResultLiveData.value=true
        })
    }

    /**
     * @Note   更改地址
     * @param  data 更改后的信息
     */
    fun editAddress(map: HashMap<String, Any>) {
        val observable = memberApi.editAddress((map.get("id") as Int), map)
        baseSubscribe(observable, SimpleNetParams()
                , Consumer {
//            providerView().addAddressResult()
            addAddressResultLiveData.value=true
            showMessage(R.string.修改成功)
        }, Consumer {
            if (it is CustomThrowable) {
                var errorCode = (it as CustomThrowable).errResultModel!!.code
                var errorMsg = (it as CustomThrowable).errResultModel!!.message
//                providerView().addAddressError(errorCode, errorMsg)
                addAddressErrorLiveData.value=it
            }
        })
    }
    val addAddressErrorLiveData=MutableLiveData<CustomThrowable>()
}